var deduction;
var diahtml = '';
$(function(){
	$.ajax({
		type:"post",
		url:url+'/user/getalipayinfo.html?token='+token,
		async:true,
		data:{'token':token},
		dataType:'json',
		success:function(result){
			console.log(result)
			if(result.status === "0001"){
				deduction = result.data.deduction;			
				if(result.data.alipay == "" && result.data.alipay_img == ""){
					diahtml = "首次修改不需要扣除SDC"
				}else{
					diahtml = '需扣除' + deduction + '个SDC,是否修改？'
				}				
				if(result.data.alipay != ""){
					$(".uploading_btn").html("添加");
				}
				$("#alipay_name").val(result.data.alipay_name);
				$("#alipay").val(result.data.alipay);
				var alipay_img = result.data.alipay_img == "" ? "../images/orderdetails_pic.png" : result.data.alipay_img;
				$("#alipay_img").attr("src",alipay_img);
			}else{
            isErr(result);
        }
		},
		error:function(){
			layer.msg("响应失败");
		}
	})

//	上传图片
$(".imgg").on("click",function(){
	$("#file1").click();
})
$("input[type='file']").on("change",function(){
	dialog("#d1")
	doUpload();
})
//上传图片接口
function doUpload() {
	dialog("#d1");
 	var formData = new FormData($( "#form1")[0]);  
 	formData.append("token",token);
 	$.ajax({  
      	url: url+'/user/upalipayimg.html?token='+token,
  		type: 'POST',  
        data: formData,
        async: false,  
        cache: false,  
        contentType: false,  
        processData: false, 
        dataType:"json",
        success: function (result) {
			dialog("#d1",2000);
        	if(result.status == '0001'){
				$(".imgg").attr("src",result.src);
				// layer.msg(result.message);
	        }else{
	            isErr(result);
	        }
        },
		error:function(){
			dialogClose();
			layer.msg("响应失败");
		}
    });  
}


//	绑定支付宝账号
var prevent_repetition=true;
	$(".uploading_btn").on("click",function(){
		if(prevent_repetition){
			prevent_repetition=false;
			var alipay=$(".alipay").val();
			var alipay_name=$(".alipay_name").val();
			var alipay_img=$(".imgg").attr("src");
			if(!alipay_name){
				layer.msg("请输入支付宝姓名");
				prevent_repetition=true;
				return false;
			}
			if(!alipay){
				layer.msg("请输入支付宝账号");
				prevent_repetition=true;
				return false;
			}
			if(alipay_img == "../images/orderdetails_pic.png"){
				layer.msg("请上传支付宝收款码");
				prevent_repetition=true;
				return false;
			}
			var d={};
			d.token=token;
			d.alipay=alipay;
			d.alipay_name=alipay_name;
			d.alipay_img=alipay_img;
			layer.alert(diahtml, {
                closeBtn: 0    // 是否显示关闭按钮
                ,anim: 6 //动画类型
                ,btn: ['确认','取消'] //按钮
                ,yes:function(){
                    $.ajax({
						type:"post",
						url:url+'/user/update.html?token='+token,
						async:false,
						data:d,
						dataType:'json',
						success:function(result){
							if(result.status == "0001"){
								
								layer.msg(result.message);
								location.href='my_information1.html';
							}else{
								isErr(result);
								prevent_repetition = true;
							}
						},
						error:function(){
							layer.msg("响应失败");
							prevent_repetition = true;
						}
					})
                }
                ,btn2:function(){
					prevent_repetition = true;
				}
            });
			
		}
	})
})
